package com.wulinbus.interceptor;

import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fancy.common.utils.StringUtil;
import com.wulinbus.utils.AdminAuthCheck;
import com.wulinbus.utils.AuthCheck;

/**
 * 权限控制和恢复拦截器
 * 
 * @author xinfeng.hu 2013-3-14下午1:44:07
 * @version 1.0.0
 * @category 杭州广桥网络技术有限公司(商业先知)
 */
public final class Interceptor extends HandlerInterceptorAdapter implements HandlerExceptionResolver {

	/**
	 * 日志记录器
	 */
	private final Logger logger = LoggerFactory.getLogger(Interceptor.class);

	/**
	 * 请求前的预处理（进行编码、安全控制等处理）
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (request.getServerName().equals("50bus.com")) {
			response.addHeader("location", "http://www.50bus.com/");
			response.setStatus(302);
			return false;
		}
		if (request.getServerName().equals("50bus.cn")) {
			response.addHeader("location", "http://www.50bus.cn/");
			response.setStatus(302);
			return false;
		}
//		Boolean logined = false;
//		Cookie[] cookies = request.getCookies();
//		for (Cookie cookie : cookies) {
//			if (cookie.getName().equals("uid")) {
//				if (!StringUtil.isEmpty(cookie.getValue())) {
//					logined = true;
//				}
//			}
//		}
//		if (logined.booleanValue() == false) {
//			AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
//			if (principal != null) {
//				Map attributes = principal.getAttributes();
//				String uid = (String) attributes.get("uid");
//				if (!StringUtil.isEmpty(uid)) {
//					 Cookie cookie = new Cookie("uid", uid.toString());
//					 cookie.setPath("/");
//					 response.addCookie(cookie);
//				}
//			}
//		}
//		
		// try{
		// HandlerMethod handlerMethod = (HandlerMethod) handler;

		// final Method method = handlerMethod.getMethod();
		//
		// // 取得请求controll方法的权限注释
		// final boolean needAuth = method.getAnnotation(AuthCheck.class) !=
		// null;
		// if (needAuth) {
		// Cookie[] cookies = request.getCookies();
		// Boolean logined = false;
		// for (Cookie cookie : cookies) {
		// if (cookie.getName().equals("uid")) {
		// if (!StringUtil.isEmpty(cookie.getValue())) {
		// logined = true;
		// }
		// }
		// }
		// if (logined == false) {
		// response.sendRedirect("/user/login.html");
		// return false;
		// }
		// }
		// }catch(Exception e){}

		try {
			Class controller = handler.getClass();
			AdminAuthCheck admin_auth = (AdminAuthCheck) controller.getAnnotation(AdminAuthCheck.class);
			if (admin_auth != null) {
				Cookie[] admin_cookies = request.getCookies();
				Boolean admin_logined = false;
				for (Cookie admin_cookie : admin_cookies) {
					if (admin_cookie.getName().equals("muid")) {
						if (!StringUtil.isEmpty(admin_cookie.getValue())) {
							admin_logined = true;
						}
					}
				}
				if (admin_logined == false) {
					response.sendRedirect("/admin/login.html");
					return false;
				}
			}
		} catch (Exception e) {
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.err.println();
	}

	@Override
	public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) {
		return null;
	}

}
